package com.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.dao.ContactDAO;
import com.pojo.Contact;

public class ContactAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	List<Contact> list = null;
	Contact contact = null;
	private List<Contact> listContactType = new ArrayList<Contact>();
	private String inActive = "false";
	private String listInactiveDir;

	private String chooseContactType;
	private String chooseBusinessMethod;

	public List<Contact> getListContactType() {
		return listContactType;
	}

	public void setListContactType(List<Contact> listContactType) {
		this.listContactType = listContactType;
	}

	public String getChooseContactType() {
		return chooseContactType;
	}

	public void setChooseContactType(String chooseContactType) {
		this.chooseContactType = chooseContactType;
	}

	public String getChooseBusinessMethod() {
		return chooseBusinessMethod;
	}

	public void setChooseBusinessMethod(String chooseBusinessMethod) {
		this.chooseBusinessMethod = chooseBusinessMethod;
	}

	public Contact getContact() {
		return contact;
	}

	public void setContact(Contact contact) {
		this.contact = contact;
	}

	public String list() throws Exception {
		String isActive;
		if (inActive.equals("false")) {
			isActive = "Yes";
		} else
			isActive = "No";
		list = new ContactDAO().list(isActive);
		return SUCCESS;
	}

	public List<Contact> getList() {
		return list;
	}

	public void setList(List<Contact> list) {
		this.list = list;
	}

	public ContactAction() {
		list = new ArrayList<Contact>();
		contact = new Contact();
	}

	public String addAction() {
		ContactDAO dao = new ContactDAO();
		String isActive;
		if (inActive.equals("false")) {
			isActive = "Yes";
		} else
			isActive = "No";
		listContactType = dao.list(isActive);
		return SUCCESS;
	}

	public String insertAction() {
		ContactDAO dao = new ContactDAO();
/*		listContactType = dao.getAll();*/
		if (contact.getIsActive().equals("true")) {

			contact.setIsActive("Yes");
		} else {
			contact.setIsActive("No");
		}
		contact.setFirstName(contact.getFirstName());
		contact.setSname(contact.getSname());
		contact.setKnowAs(contact.getKnowAs());
		contact.setOfficePhone(contact.getOfficePhone());
		contact.setBestContactMethod(chooseBusinessMethod);
		contact.setBestContactMethod(contact.getBestContactMethod());
		contact.setMobilePhone(contact.getMobilePhone());
		contact.setHomePhone(contact.getHomePhone());
		contact.setEmailAddr(contact.getEmailAddr());
		contact.setContactType(chooseContactType);
		contact.setContactType(contact.getContactType());
		contact.setJobRole(contact.getJobRole());
		contact.setWorkBase(contact.getWorkBase());
		contact.setJobTitle(contact.getJobTitle());
		contact.setIsActive(contact.getIsActive());
		contact.setManagerName(contact.getManagerName());
		dao.insertContact(contact);
		return SUCCESS;
	}

	public String updateAction() {

		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(ServletActionContext.HTTP_REQUEST);
		int contactId = Integer.parseInt(request.getParameter("contactId"));

/*		listContactType = new ContactDAO().getAll();*/

		if (contact.getIsActive().equals("true")) {

			contact.setIsActive("Yes");
		} else {
			contact.setIsActive("No");
		}
		contact.setFirstName(contact.getFirstName());
		contact.setSname(contact.getSname());
		contact.setKnowAs(contact.getKnowAs());
		contact.setOfficePhone(contact.getOfficePhone());
		contact.setBestContactMethod(chooseBusinessMethod);
		contact.setBestContactMethod(contact.getBestContactMethod());
		contact.setMobilePhone(contact.getMobilePhone());
		contact.setHomePhone(contact.getHomePhone());
		contact.setEmailAddr(contact.getEmailAddr());
		contact.setContactType(chooseContactType);
		contact.setContactType(contact.getContactType());
		contact.setJobRole(contact.getJobRole());
		contact.setWorkBase(contact.getWorkBase());
		contact.setJobTitle(contact.getJobTitle());
		contact.setIsActive(contact.getIsActive());
		contact.setManagerName(contact.getManagerName());
		new ContactDAO().updateContact(contact, contactId);
		return SUCCESS;
	}

	public String edit() {
		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(ServletActionContext.HTTP_REQUEST);
		int contactId = Integer.parseInt(request.getParameter("contactId"));
		contact = new ContactDAO().getContactById(contactId);
		String isActive;
		if (inActive.equals("false")) {
			isActive = "Yes";
		} else
			isActive = "No";
		listContactType = new ContactDAO().list(isActive);
		if (contact.getIsActive().equals("true")) {

			contact.setIsActive("Yes");
		} else {
			contact.setIsActive("No");
		}
		return SUCCESS;
	}

	public String deleteAction() {
		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(ServletActionContext.HTTP_REQUEST);
		int contactId = Integer.parseInt(request.getParameter("contactId"));
		new ContactDAO().deleteContact(contactId);
		return SUCCESS;
	}

	public String filterIsActive() throws Exception {
		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(ServletActionContext.HTTP_REQUEST);
		String filterName = request.getParameter("filterName");
		request.getSession().setAttribute("filter", filterName);
		String isActive;
		if (inActive.equals("false")) {
			isActive = "Yes";
		} else
			isActive = "No";
		list = new ContactDAO().getByFilterName(filterName, isActive);
		return SUCCESS;
	}
	public String getListInactiveDir() {
		return listInactiveDir;
	}

	public void setListInactiveDir(String listInactiveDir) {
		this.listInactiveDir = listInactiveDir;
	}

	public String getInActive() {
		return inActive;
	}

	public void setInActive(String inActive) {
		this.inActive = inActive;
	}

}
